package org.fly.controller.admin;

import org.fly.bean.Classification;
import org.fly.bean.query.ClassificationQuery;
import org.fly.bean.warpper.ClassificationTreeWarpper;
import org.fly.bean.warpper.ClassificationWarpper;
import org.fly.controller.AbstractController;
import org.fly.service.ClassificationService;
import org.fly.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * author fly
 */
@Controller
@RequestMapping("/admin/classification/")
public class AdminClassificationController extends AbstractController{

    @Autowired
    ClassificationService classificationService;
    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(ClassificationQuery query){
        List<ClassificationWarpper> list =  classificationService.listWarpper(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(ClassificationQuery query){
        long count =  classificationService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }

    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(Classification classification, HttpServletRequest request){
        try {
            classificationService.save(classification);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        Classification bean = classificationService.findById(id);
        return toJSONString(createDataTip(bean));
    }

    @RequestMapping(value = "listByParentId", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String listByParentId(String parentId){
        List<Classification> list =  classificationService.listByParentId(parentId);
        return toJSONString(createDataTip(list));
    }

    @RequestMapping(value = "treeMap", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String treeMap(){
        List<ClassificationTreeWarpper> list =  classificationService.treeMapAll();
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "delete",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(String id){
         classificationService.delete(id);
        return toJSONString(createDataTip(SUCCESS));
    }
}
